<?php
class photo {
	
	public $base;
	public $photo;
	public $vilidation;
	
	/**
	 * 类的初始化
	 *
	 * @param oject $base
	 * 加载photo视图
	 * 加载类库upload
	 */
	function __construct($base) {
		$this->base = $base;
		//加载模型photo
		$this->photo = $this->base->loadModel('photo',$this->base);
	  //  $this->upload = $this->base->loadLib('upload');
	 	//加载类库validation
	 	$this->vilidation = $this->base->loadLib('validation');
	}
	
	public function index() {
		$data['upload_dir'] = $this->base->config->uploadDir;
		$data['new_photo'] = $this->photo->getNewPhoto();
		$data['album_name'] = $this->photo->getUserAlbum();
		$this->base->loadView('photo',$data);
		
	}
	
	/**
	 * 图片上传函数
	 * 上传总大小为2M 一次不超过5张
	 *note 每一次上传照片的数量如何处理？note:count($_FILES['image']['name'])
	 *空数组怎么解决？刷新重传怎么解决？只能上传部分图片？
	 */
	public function upload() {
		if(isset($_POST['Submit'])) {
			//上传文件存放的最终路径
			$uploadDir = $this->base->config->uploadDir; 
			//上传图片允许的类型
			$allowed = array('image/gif','image/jpeg','image/jpg','image/pjpeg','image/png');
			//一次上传的张数 最多可以传5张
			$counter = count($_FILES['image']['name']);
			               
			if(isset($_FILES['image'])) {
				//foreach( $_FILES['image'] as $key =>$value) echo $key,$value;
				//echo $_FILES['image']['name'][0];
				for ($i=0;$i<$counter && $counter < 6;$i++) {
					//当日的上传图片放在当日的文件夹中
					$today = date('Y-m-d');
					//echo $_FILES['image']['type'][$i];
					if (!is_dir($uploadDir.'\\'.$today)) mkdir($uploadDir.'\\'.$today);
					//上传图片的名字、类型、大小
					//$name = $_FILES['image']['name'][$i];
					//$type = $_FILES['image']['type'][$i];
					//$size = $_FILES['image']['size'][$i];

					//判断上传图片的类型和大小
					if(in_array($_FILES['image']['type'][$i],$allowed) && $size < 200000) {
						//将数据写入数据库，返回 upload_id
						$upload_id = $this->photo->insertToUpload($_FILES['image']['name'][$i],$_FILES['image']['type'][$i],$_FILES['image']['size'][$i]);
						//将文件移到web目录以外,并重命名
						$result = move_uploaded_file($_FILES['image']['tmp_name'][$i],$uploadDir.'\\'.$today."/$upload_id");
						if($result) {
							//将文件的路径写入数据库中
							$path = $uploadDir.'\\'.$today.'\\'."$upload_id";
							$this->photo->updatePathById($path,$upload_id);
							//返回成功信息的页面
							$data['upload_id'] = $upload_id;
							//echo "<img src=\" $uploadDir.'\\'.$today.'\\'.$upload_id"\" >";
							$data['path'] =  $path;
							$this->base->loadView('upload',$data);
						} else {
							//将写入数据库的上传信息删除
							$this->photo->deleteUploadById($upload_id);
							//返回错误信息
							switch ($_FILES['image']['error'][$i]) {
								case 1 :
								echo	$error = '上传的文件大小超出了upload_max_filesize指令指定的值';
									break;
								case 2 :
								echo 	$error = '上传的文件超出了upload_file_size指令指定的值';
									break;
								case 3 :
								echo 	$error = '文件只上传了一部分，没有完全上传';
									break;
								case 4 :
								echo	$error = '没有上传文件';
									break;
								default:
								echo	$error = '系统错误';
									break;
							}
							
						}
						
					} else {
						 echo '您只能传gif,jpeg,jpg,pjpeg,png 的图片，单张大小不能超过2M';
					}
						
				}					
			} 
		} else {
			echo '请您选择一张图片';
		}
	}
	public function uploadone() {
		define('UPLOADDIR',"C:\\uploads");//上传文件存放的最终路径
		$counter = 5;//一次上传的图片的张数
		
		$allowed = array('image/gif','image/jpeg','image/jpg','image/pjpeg');//上传图片允许的类型
		
		if (isset($_FILES['image'])){
			for ($i=0;$i<$counter;$i++) {
				echo 'hao';echo $_FILES['image']['type'][$i];
				if(in_array($_FILES['image']['type'][$i],$allowed)) {
					$today = date('Y-m-d');
					$name= $_FILES['image']['name'][$i];
					$type = $_FILES['image']['type'][$i];
					$size = $_FILES['image']['size'][$i];
					$path = UPLOADDIR.'\\'.$today;
					
					if (!is_dir(UPLOADDIR.'\\'.$today)) mkdir(UPLOADDIR.'\\'.$today);
						$result = move_uploaded_file($_FILES['image']['tmp_name'][$i],UPLOADDIR.'\\'.$today."/{$_FILES['image']['name'][$i]}");
						if($result) {
							//将数据写入数据库，返回成功信息
							$id = $this->photo->insertToUpload($name,$type,$size,$path);
							$data['name'] = $_FILES['image']['name'][$i];
							$this->base->loadView('upload',$data);
							
							echo "success";
							echo $id;
						} else {
							//错误信息
							switch ($_FILES['image']['error'][$i]) {
								case 1 :
								echo	$error = '上传的文件大小超出了upload_max_filesize指令指定的值';
									break;
								case 2 :
								echo 	$error = '上传的文件超出了upload_file_size指令指定的值';
									break;
								case 3 :
								echo 	$error = '文件只上传了一部分，没有完全上传';
									break;
								case 4 :
								echo	$error = '没有上传文件';
									break;
								default:
								echo	$error = '系统错误';
									break;
								}
						}	
					
					
					//else echo "<p>failed</p>";
				}
				
			}
		} else {
			
		}
	}
	/**
	*对批量上传的图片贴标签
	*
	*/
	function uploadTags() {
	}
	/**
	*对单个上传图片的描述
	*数据的验证 没完成
	*@ tag desc priv
	*/
	public function oneUpDesc() {
		$upload_id = $_POST['upload_id'];
		$tag = $this->vilidation->escape_data($_POST['tag']);
		$desc = $this->vilidation->escape_data($_POST['desc']);
		$priv = $this->vilidation->escape_data($_POST['privilege']);
		if(!empty($tag)) {
			$tagname = explode(" ", $tag);
			foreach ($tagname as $key=>$value) {
				if(!empty($value)) {
				 	//查找标签是否存在，不存在则写入
				 	//if($this->photo->getTagByName($value))
				 echo	$tag_id = $this->photo->insertToTag($value);
					//上传的图片与标签的ID写入数据库中
					$this->photo->insertToUploadTag($upload_id,$tag_id);	
					//$this->photo->insertToAlbumAndTag($album_id,$tag_id);
			 	}
			}
		}
		if(!empty($desc)) {
			//对上传图片的描述
			$this->photo->insertToDesc($desc,$upload_id);
		}
		if(!empty($priv)) {
			//更改数据库表upload之中的权限
			$this->photo->updateUpById($upload_id,$priv);
		}
		
	}
	/**
	 *ajax 创建相册
	 * note:因为数据库乱码 还没有做数据库已有相册名和标签的验证  如何将相册的id传给upload表单
	 */
	function createAlbum() {
		$title = $this->vilidation->escape_data($_POST['title']);
		$tag = $this->vilidation->escape_data($_POST['tag']);
		if (!empty($_POST['description'])) {
			$desc = $this->vilidation->escape_data($_POST['description']);
		} else {
			$desc = null;
		}
		$priv = $this->vilidation->escape_data($_POST['privilege']);
		if (!empty($title)) {
			$this->photo->getAlbumName($title);
			$album_id = $this->photo->insertToAlbum($title,$desc,$priv);	
			$tagname = explode(" ", $tag);
			foreach ($tagname as $key=>$value) {
			 	if (!empty($value)) {
			 		//if($this->photo->getTagByName($value))
			 		$tag_id = $this->photo->insertToTag($value);
					$this->photo->insertToAlbumAndTag($album_id,$tag_id);
			 	}
		}
			echo $this->photo->getUserAlbum();
		}
		//将标签插入到数据库表tag中
		/*$tagname = explode(" ", $tag);
		 foreach ($tagname as $key=>$value) {
		 	if (!empty($value)) {
		 		//if($this->photo->getTagByName($value))
		 			$this->photo->insertToTag($value);
		 	}
		}
		*/
	}

}
?>